home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v2.1 / Amiga Developer CD v2.1.iso / CDTV / cdtvtools-11 / debox / simple.c < prev   
C/C++ Source or Header  |  1991-06-24  |  5KB  |  180 lines

  1. /* :ts=4
  2. *
  3. *    simple.c -- a simple debox picture displayer
  4. *
  5. *    William A. Ware                            9007.20
  6. *
  7. *****************************************************************************
  8. *   This information is CONFIDENTIAL and PROPRIETARY                        *
  9. *   Copyright (C) 1990, Silent Software Incorporated.                        *
  10. *   All Rights Reserved.                                                    *
  11. ****************************************************************************/
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <libraries/dos.h>
  16. #include <graphics/gfxbase.h>
  17. #include <graphics/view.h>
  18. #include "debox.h"
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/graphics.h>
  22. #include "deboxproto.h"
  23.  
  24.  
  25. struct GfxBase *GfxBase;
  26. struct Library *DeBoxBase;
  27.  
  28. BPTR                outfile;    /* for stdout outputs */
  29.  
  30. BPTR                  file;
  31. struct CompHeader    header;
  32. UBYTE                *indata;        /* the body of the data */
  33. LONG                insize;            /* the size of the body */
  34. struct BMInfo         *bmi;            /* info on the bitmap */
  35. struct BitMap         *bm;
  36. UBYTE                *mask;
  37. LONG                planesize;
  38. struct View              *view;
  39. struct View            *oldview;
  40.  
  41.  
  42. void cleanexit( int rv, char *exitmsg )
  43. {
  44.     if (rv != 0) 
  45.     {
  46.         if (outfile)
  47.         {
  48.             Write( outfile, exitmsg, strlen(exitmsg) );
  49.             Write( outfile, "\n", 1);
  50.         }
  51.     }
  52.     if (oldview) LoadView(oldview);
  53.     if (view)     DeleteView((struct SuperView *)view);
  54.     if (mask)     FreeMem( mask, planesize );
  55.     if (bm)         FreeBitMap(bm);
  56.     if (bmi)     FreeBMInfo(bmi);
  57.     if (indata)     FreeMem(indata,insize);
  58.     if (file)     Close(file);
  59.     if (DeBoxBase)    CloseLibrary(DeBoxBase);
  60.     if (GfxBase)     CloseLibrary(GfxBase);
  61.     
  62.     exit(rv);
  63. }
  64.  
  65.  
  66. main(int argc,char **argv)
  67. {
  68.     LONG i;
  69.         /* Picture Information */
  70.  
  71.     outfile = Output();
  72.     if (argc != 2) 
  73.         cleanexit(5,"Usage: simple <file>");
  74.     
  75.     /* Open Libraries -- Gfx and Debox */
  76.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",1L)))
  77.         cleanexit(10,"Unable to open graphics.library.");
  78.     if (!(DeBoxBase = OpenLibrary("debox.library",0L)))
  79.         cleanexit(10,"Unable to open debox.library.");
  80.     
  81.     if (!(file = Open(argv[1],MODE_OLDFILE)))     
  82.         cleanexit(5,"Unable to open input file.");
  83.  
  84.         /* First read a header into a buffer */
  85.     if (Read(file,(char *)&header,COMPHEADER_SIZE) != COMPHEADER_SIZE)
  86.         cleanexit(5,"Unable to read header.");
  87.     
  88.         /* This is not really required.  It just a check to see if the
  89.          * data is for the proper version.  */
  90.     if (HeaderSize( &header ) != COMPHEADER_SIZE) 
  91.         cleanexit(5,"Header Version inccorrect.");
  92.  
  93.         /* Another Check -- This time for a picture. (not required, it is
  94.          * safe not to check ) */
  95.     if (header.ci_Type != DEBOXTYPE_PIC)
  96.         cleanexit(5,"File not a debox picture.");
  97.  
  98.         /* NEW - header.ci_CSize will be the correct size */
  99.     insize = header.ci_CSize;
  100.  
  101.         /* Allocate and load */
  102.     if (!(indata = AllocMem( insize, MEMF_PUBLIC )))
  103.         cleanexit(5,"Unable to allocate memory for the file.");
  104.     if (Read(file,(char *)indata,insize) != insize )
  105.         cleanexit(5,"Unable to read the file into memory.");
  106.  
  107.         /* Now the picture is ready to be decompressed.  First however
  108.          * the information on the bitmap needs to be retrieved.
  109.          * If this routine fails then the file is not a sbox - picture file 
  110.          * or it is a corrupted picture file.
  111.          */
  112.     if (!(bmi = DecompBMInfo( NULL,&header, indata ))) 
  113.         cleanexit(5,"Error in data.");
  114.         
  115.         /* Now the bitmap and view port can be made with this information */
  116.     
  117.     if (!(bm = AllocBitMap(bmi->bmi_Depth,bmi->bmi_Width,bmi->bmi_Height)))
  118.         cleanexit(5,"Unable to allocate a bitmap.");
  119.     
  120.         /* If there is a mask - need an extra plane. */
  121.     planesize = bm->Rows * bm->BytesPerRow;
  122.     if (bmi->bmi_Flags & BMIF_HAS_MASK)
  123.     {
  124.         if (!(mask = AllocMem( planesize, MEMF_CHIP )))
  125.             cleanexit( 5,"Unable to allocat memory for mask.");
  126.     }
  127.     
  128.         /* CREATE THE VIEW - Allocated also (thats what the NULL is for) */
  129.     if (!(view=(struct View *)CreateView(NULL,bm,bmi->bmi_Width,
  130.                                         bmi->bmi_Height,bmi->bmi_Modes)))
  131.         cleanexit(5,"Unable to allocate view structures.");
  132.  
  133.             /* Load the Color Map if availible */
  134.     if (bmi->bmi_ColorMap)
  135.         LoadRGB4(view->ViewPort,bmi->bmi_ColorMap,bmi->bmi_NumColors);
  136.  
  137.             /* Now I am going to decompress the bitmap. */
  138.     if (DecompBitMap(&header,indata,bm,mask) < 0)
  139.         cleanexit(5,"Error in data.");
  140.  
  141.             /* SAVE the old view and load the new view */
  142.     oldview = GfxBase -> ActiView;    
  143.     LoadView( view );
  144.     
  145.     if (bmi->bmi_ColorMap && bmi->bmi_RangeInfo)
  146.     {
  147.         /* I Have a colormap and some ranges - Do some Color cycling */
  148.         
  149.         /* I Could put this into a Vertical Interrupt - Since I Just want
  150.            to cycle the colors I am just do a simple loop */
  151.         for(i=0;i<240;i++)
  152.         {
  153.             WaitTOF();
  154.                 /* 16666 - is the micro seconds for 60 frames a second.
  155.                  *         The PAL value would be 20000 for 50 fps. */
  156.             if (CycleColors( bmi, (LONG)16666 ))
  157.                 LoadRGB4(view->ViewPort,bmi->bmi_ColorMap,bmi->bmi_NumColors);
  158.                 /* If CycleRanges() Returns != 0 then The colortable has
  159.                    changed */
  160.         }
  161.     }
  162.     else
  163.     {    /* No Ranges or colormap - Just sit for 4 seconds */
  164.         Delay(240L);
  165.     }
  166.  
  167.         /* If There is a mask Show it, to prove that it is there */
  168.     if (mask)
  169.     {
  170.         int i;
  171.         CopyMem(mask,bm->Planes[0],planesize);
  172.         for(i=1;i<bm->Depth;i++) MemSet(bm->Planes[i],0,planesize);
  173.         SetRGB4( view->ViewPort, 0, 0,0,0 );
  174.         SetRGB4( view->ViewPort, 1, 0xf,0xf,0xf );
  175.         Delay(120L);
  176.     }
  177.     
  178.     cleanexit( 0, NULL );
  179. }
  180.